// pages/news/news.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    baseImgUrl:app.$http.baseImgUrl,
    news:{},
    newsUser:{},
    height:wx.getSystemInfoSync().windowHeight,
    pageNum:1,
    total:0,
    triggered:true
  },
  //刷新
  onRefresh(){
    this.getData()
    this.setData({
       triggered:false,
       pageNum:1,
       total:0,
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({id:options.id})
    this.getData()
  },
  //网络请求，获取数据
  getData(){
    //调用接口获取新闻内容
    app.$http.get('/app/news/'+this.data.id).then(res=>{
      this.setData({
        news:res.data,
        content:res.data.content.replaceAll('src="/images','style="height:100%;width:100%;border-radius: 5px;" mode="aspectFit" src="' + this.data.baseImgUrl)
      })
      //调用接口获取新闻响应内容
      app.$http.get('/app/respond/list',{pageNum:this.data.pageNum,pageSize:2,newsId:this.data.id,status:1}).then(resp=>{
        this.setData({
          respondList:resp.data.records,
          total:resp.data.total
        })
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      }).catch(err=>{console.log(err)})
    }).catch(err=>{console.log(err)})
  },
  //网络请求，添加数据
  appendData(){
    app.$http.get('/app/respond/list',{pageNum:this.data.pageNum+1,pageSize:2,newsId:this.data.id,status:1}).then(resp=>{
      this.setData({
        pageNum:this.data.pageNum+1,
        respondList:this.data.respondList.concat(resp.data.records)
      })
      
    }).catch(err=>{console.log(err)})
  },
  //编辑响应内容
  toWriteRespond:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({url: "/pages/news/writeRespond/writeRespond?id="+id})
  },
  //滑动到底部
  lower(){
    if (this.data.total > this.data.respondList.length) {
      this.appendData()
    }
    console.log('滑动到底部')
  },
})